package com.travel.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.travel.manager.dao.HotelDAO;
import com.travel.manager.service.HotelService;
import common.pojo.Hotel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.*;
import java.util.List;

@Service
public class HotelServiceImpl implements HotelService {
    @Autowired
    private HotelDAO hotelDAO;
    @Autowired
    private JmsTemplate jmsTemplate;
    @Resource
    private Destination destination;

    public boolean doUpdateIndex(Integer hotelId) {
        try {
            jmsTemplate.send(destination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    TextMessage textMessage = session.createTextMessage(hotelId.toString());
                    return textMessage;
                }
            });
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public void addHotel(Hotel hotel) {
        hotelDAO.addHotel(hotel);
        doUpdateIndex(hotel.getId());
    }

    @Override
    public void updateHotel(Hotel hotel) {
        hotelDAO.updateHotel(hotel);
        doUpdateIndex(hotel.getId());
    }

    @Override
    public PageInfo<Hotel> getHotelList(Hotel hotel, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);//设置分页信息
        List<Hotel> hotelList = hotelDAO.getHotelList(hotel);
        PageInfo<Hotel> pageInfo = new PageInfo<>(hotelList);
        return pageInfo;
    }

    @Override
    public Hotel getHotelById(Integer id) {
        return hotelDAO.getHotelById(id);
    }

}
